﻿using ProjectCloth.Entity;
using ProjectCloth.FrmUI.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ProjectCloth.FrmUI
{
    public partial class ReceivableAccountListFrm : UserControl
    {
        private ClothContext db = new ClothContext();
        private MainFrm mainFrm;
        private TabControl tabControl;
        public ReceivableAccountListFrm()
        {
            InitializeComponent();
            var mainFrm = MainFrm.CreateInstrance();
            var tab = mainFrm.Controls.Find("tabControl1", true).FirstOrDefault() as TabControl;
            //var tabControl = tab;
            if (tab != null)
            {
                tabControl = tab as TabControl;
            }
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void searchBtn_Click(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            var start = startDatePiker.Value;
            var end = endDatePicker.Value;
            var status = IsInstockStatus.Checked ? 1 : 0;
            var isreceived = IsReceiveChx.Checked ? 1 : 0;
            var sql = from re in db.ReceivableAccounts.Where(o => o.MakerTime > start && o.MakerTime < end && o.InstockStatus == status && o.IsReceived == IsReceiveChx.Checked)//.ToList();
                      select new
                      {
                          InstockStatus = re.InstockStatus == 0 ? "未" : "已",
                          Instock = re.InstockStatus == 0 ? "进货" : "已进货",
                          re.MakerName,
                          re.ReceivedAccountName,
                          re.MakerTime,
                          re.Remark,
                          re.Summary,
                          re.Id,
                          re.PayCompany,
                          re.Amount,
                          IsReceived = re.IsReceived == true ? "已收" : "未收",
                          re.RelatedNo,
                          //ReceiveBtn = re.IsReceived ? Disable : Enable
                      };
            dataGridView1.DataSource = sql.ToList();
            //MessageBox.Show("暂时没有数据！");
        }

        private void exportBtn_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //自动编号，与数据无关
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
               e.RowBounds.Location.Y,
               dataGridView1.RowHeadersWidth - 4,
               e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                  (e.RowIndex + 1).ToString(),
                   dataGridView1.RowHeadersDefaultCellStyle.Font,
                   rectangle,
                   dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
                   TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0 && dataGridView1.CurrentRow != null)
            {
                var exId = dataGridView1.CurrentRow.Cells["Id"].Value;
                if (exId != null && (int)exId > 0)
                {
                    var itemList = from re in db.ReceivableAccountItems.Where(o => o.ReceivableAccountId == (int)exId).ToList()//from gei in
                                   join pr in db.Products on re.ProductId equals pr.Id
                                   select new
                                   {
                                       ProductNo = pr.ProductNo,
                                       ColorType = re.ColorType,
                                       //修改价格变动后，显示的价格错误，直接通过计算
                                       Price = re.Quantity != 0 ? re.Amount / re.Quantity : 0,// re.Unit == 0 ? pr.Price : pr.BatchPrice,
                                       Unit = re.Unit,
                                       Quantity = re.Quantity,
                                       ItemRemark = re.Remark,
                                       ItemId = re.Id,
                                       ItemAmount = re.Amount,
                                       ReceivableAccountId = re.ReceivableAccountId
                                   };

                    dataGridView2.AutoGenerateColumns = false;
                    dataGridView2.DataSource = itemList.ToList();
                }
                DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
                if (column is DataGridViewButtonColumn && exId != null && (int)exId > 0)
                {

                    var name = column.DataPropertyName;
                    var receivableAccounts = db.ReceivableAccounts.Find((int)exId);
                    if (receivableAccounts != null && name == "ReceiveBtn")
                    {
                        if (receivableAccounts.IsReceived == true)
                        {
                            MessageBox.Show("已收款！无需操作。");
                        }
                        else
                        {
                            var frm = new ReceivableAccountFrm(receivableAccounts.Id);
                            new AddTab(frm, "ReceivableAccountFrm", "收款单", tabControl);
                            searchBtn_Click(null, null);
                            //frm.Show();
                            //var oldAmount = account.Amount;
                            //account.OriginalAmount = oldAmount;
                            //account.Amount += receivalbe.Amount;
                            //account.UpdateTime = DateTime.Now;
                            //var log = new CashAccountLog()
                            //{
                            //    CashAccountId = account.Id,
                            //    CreateTime = DateTime.Now,
                            //    CurrentAmount = account.Amount,
                            //    OriginalAmount = oldAmount,
                            //    Remark = RemarkTxt.Text,
                            //    MakerId = makerCbx.SelectedValue != null ? (int)makerCbx.SelectedValue : 0,
                            //    MakerTime = inmoneyTime.Value,
                            //    RelatedObject = companyTxt.Text,
                            //    Source = "收款单",
                            //    VariableAmount = receivalbe.Amount,
                            //    LoginId = LoginInfo.GetLoginInfo().UserId,
                            //};
                            //db.CashAccountLogs.Add(log);
                            //db.SaveChanges();
                        }

                    }
                    else if (receivableAccounts != null && receivableAccounts.InstockStatus == 0)
                    {
                        receivableAccounts.InstockStatus = 1;
                        var result = db.SaveChanges();
                        if (result > 0)
                        {
                            MessageBox.Show("修改进货状态成功！");
                            searchBtn_Click(null, null);
                        }
                        else { MessageBox.Show("修改进货状态失败！"); }
                    }
                    else
                    {
                        MessageBox.Show("不是未进货状态！");
                    }

                }
            }
        }
    }
}
